package com.study.get.jsl.ths;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.study.get.jsl.BeileizhenDemo.TenTool;
import com.study.get.jsl.ths.GetHot.ThsInter.ThsHotGnInfoDTO;
import com.study.get.jsl.ths.GetHot.ThsInter.ThsHotTop;
import com.study.utils.CookieKey.ThsCookie;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.util.List;

/**
 * @author zhuzi
 * 2025/2/7 上午11:09
 * 同花顺热点轮动
 */
public class GetHot {

    public static void main(String[] args) {
        //hotBuyCode();
        //hotTop();

    }


    /**
     * 轮动首页
     *
     * @return
     */
    public static List<ThsHotTop> hotTop() {
        String url = "https://eq.10jqka.com.cn/pick/block/block_hotspot/hotspot/v1/recent_hot_block?type=con&field=zf&days=1";
        HttpHeaders headers = new HttpHeaders();
        headers.set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36");
        headers.set("Host", "eq.10jqka.com.cn");
        headers.set("Accept", "*/*");
        // 这几个不可少
        headers.set("Accept-Language", "zh-Hans-CN;q=1.0");
        headers.set("Content-Type", "application/json");

        headers.set("Cookie", "v=" + ThsCookie.thsCookie());

        String block = TenTool.webClient.get().uri(url).headers(httpHeaders -> httpHeaders.addAll(headers)).retrieve().bodyToMono(String.class).timeout(Duration.ofSeconds(8)).retry(3).block();

        String arrListStr = JSON.parseObject(block).getJSONObject("data").getString("data_list");
        List<ThsHotTop> thsHotTops = JSON.parseArray(arrListStr, ThsHotTop.class);
        thsHotTops.forEach(ia -> {
            ia.buildInfo();

            String code = StrUtil.format("{}:{}", ia.getMarket(), ia.getCode());
            List<ThsHotGnInfoDTO> dtoList = hotBuyCode(code);
            ia.setDtoList(dtoList);

        });

        return thsHotTops;
    }

    /**
     * 成分股
     */
    @Test
    public void cfg() {
        List<ThsHotGnInfoDTO> thsHotTops = hotBuyCode("");
        System.out.println(JSON.toJSONString(thsHotTops));
    }

    /**
     * 概念详情
     */
    public static List<ThsHotGnInfoDTO> hotBuyCode(String code) {
        String url = "https://dataq.10jqka.com.cn/fetch-data-server/fetch/v1/specific_data";
        String bodyTmp = "{\"sort\":[{\"idx\":2,\"type\":\"DESC\"}],\"page_info\":{\"page_begin\":0,\"code_begin\":0,\"page_size\":115},\"indexes\":[{\"index_id\":\"security_name\"},{\"index_id\":\"last_price\"},{\"index_id\":\"hq-fncdict-199112\"},{\"index_id\":\"hq-fncdict-3250\"}],\"code_selectors\":{\"intersection\":[{\"type\":\"hq_block_code\",\"values\":[\"{}\"]}]}}";

        HttpHeaders headers = new HttpHeaders();
        headers.set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36");
        headers.set("Host", "dataq.10jqka.com.cn");
        headers.set("Accept", "*/*");
        // 这几个不可少
        headers.set("Platform", "mobileweb");
        headers.set("Accept-Language", "zh-Hans-CN;q=1.0");
        headers.set("Source-id", "constituent-stock");
        headers.set("Content-Type", "application/json");

        String block = TenTool.webClient.post().uri(url).headers(httpHeaders -> httpHeaders.addAll(headers)).bodyValue(StrUtil.format(bodyTmp, code)).retrieve().bodyToMono(String.class).timeout(Duration.ofSeconds(8)).retry(3).block();

        String strList = JSON.parseObject(block).getJSONObject("data").getString("data");
        List<ThsHotGnInfoDTO> dtoList = JSON.parseArray(strList, ThsHotGnInfoDTO.class);

        dtoList.forEach(da -> {
            da.build();
        });

        return dtoList;
    }

    public interface ThsInter {
        @Data
        @Accessors(chain = true)
        class ThsHotTop {
            private String code;
            private String market;
            //private String type;
            private String name;
            private ThsHotTopInfo info;
            private int rank;

            // 以下部分是自定义
            /**
             * 涨幅
             */
            private BigDecimal zf;
            /**
             * 成交额
             */
            private BigDecimal zljlr;

            List<ThsHotGnInfoDTO> dtoList;

            public void buildInfo() {
                this.zf = NumberUtil.toBigDecimal(info.getZf()).setScale(2, RoundingMode.HALF_UP);
                this.zljlr = NumberUtil.toBigDecimal(info.getZljlr()).divide(new BigDecimal("100000000")).setScale(2, RoundingMode.HALF_UP);
            }

            public String printStr() {
                return StrUtil.format("{}|涨幅|{}|{}{}亿", name, zf, NumberUtil.isGreater(zljlr, new BigDecimal(0)) ? "净买💰" : "净卖", zljlr.abs());
            }
        }

        @Data
        @Accessors(chain = true)
        class ThsHotTopInfo {
            /**
             * 涨幅
             */
            private String zf;
            //private String rankZf;
            /**
             * 成交额
             */
            private String zljlr;
        }

        /**
         * 同花顺 轮动-详细全部
         */
        @Data
        @Accessors(chain = true)
        class ThsHotGnInfoDTO {
            private String code;

            private String codeShort;
            private List<ThsHotGnValue> values;


            //以下是自己加的
            /**
             * 名称
             */
            private String name;
            /**
             * 当日涨幅
             */
            private BigDecimal incr;
            /**
             * 五日涨幅
             */
            private BigDecimal incr5;

            public ThsHotGnInfoDTO setCode(String code) {
                this.code = code;
                this.codeShort = StrUtil.subAfter(code, ':', false);
                return this;
            }

            public void build() {
                if (CollectionUtils.isEmpty(values)) {
                    return;
                }
                values.forEach(ia -> {
                    if (0 == ia.getIdx()) {
                        this.name = ia.getValue();
                    }
                    if (2 == ia.getIdx()) {
                        this.incr = NumberUtil.toBigDecimal(ia.getValue());
                    }
                    if (3 == ia.getIdx()) {
                        this.incr5 = NumberUtil.toBigDecimal(ia.getValue());
                    }
                });
            }

            public String printStr() {
                return StrUtil.format("{}:{} 涨幅{}|五日涨幅{}", codeShort, StrUtil.fillAfter(name, ' ', 5), incr.setScale(2, RoundingMode.HALF_UP), incr5.setScale(2, RoundingMode.HALF_UP));
            }
        }

        @Data
        @Accessors(chain = true)
        class ThsHotGnValue {

            private int idx;
            private String value;
        }
    }

}
